<template>
    <div>
        <div class="main" @click="hiddenCart">
            <div :class="{'shadow':showShadow}"></div>
            <comeback :getURL ="goType"></comeback>
            <div class="image">
                <div class="good-img">
                    <img :src="imgSrc">
                    <div class="title">{{goods.title}}</div>
                </div>
                <!-- 缩略图 -->
                <div class="img_small">
                    <span v-for="(img,index) of goods.imgs" :class="{'active':index===mark}" @click="change(img,index)" :key="index">
                        <img :src="img">
                    </span>
                </div>
                <div class="goods-info">
                    <span class="price red">￥{{showPrice}}</span>
                    <span class="">销量：<span class="red">{{goods.sell || 0}}</span></span>
                    <span class="stock">库存：<span class="red">{{goods.stock}}</span></span>
                </div>
            </div>
            <div class="content">
                <div class="commodity-info">商品信息</div> 
                <div class="detail">
                    <div class="detail-info">
                        <div>
                            <span class="detail-title">名称：</span>
                            <span>{{goods.name}}</span>
                        </div>
                        <div>
                            <span class="detail-title">规格：</span>
                            <span>{{goods.specifications}}</span>
                        </div>
                        <div>
                            <span class="detail-title">信息：</span>
                            <span>{{goods.information}}</span>
                        </div>
                    </div>
                    <div class="detail-img" v-for="(img,index) of goods.bigimg" :key="index">
                        <img :src="img" v-preview="img">
                    </div>
                </div>
            </div>
        </div>
        <!-- 底部 -->
        <dl class="buy">
            <dd class="collection" @click="collect">
                <img :src="collectSrc" :class="{'collected':collectedAnimation}" alt="">
                <span>收藏</span>
            </dd>
            <dd class="shoppingcart" @click="showCart" v-if="isJoinCart">加入购物车</dd>
            <dd class="buynow" @click="showBuy" :class="{onlyBuy: !isJoinCart}">立即购买</dd>
        </dl>
        <!-- 加入购物车 -->
        <transition name="cart">
        <div class="cart" v-if="isShow">
            <div class="selectSpecifications">
                <div class="specifications">
                    <span class="cart-title">规格</span>
                    <div class="all-specifications">
                        <span v-for="(spe,index) of goods.specificationsCart" :key="index" :class="{'selected':index===cart.option}" @click="select(index)">{{spe}}</span>
                    </div>
                </div>
                <div class="count cart-info">
                    <span class="cart-title">数量</span>
                    <span @click="minus" :class="{'minus':cart.count<=1}">-</span>
                    <span class="cart-count">{{cart.count}}</span>
                    <span @click="plus">+</span>
                </div>
                <div class="price_ cart-info">
                    <span class="cart-title">金额</span>
                    <span>￥{{cart.totalPrice}}</span>
                </div>
            </div>
            
            <span class="push-cart" @click="addToCart">{{cartOrBuy}}</span>
        </div>
        </transition>
        <lg-preview></lg-preview>
    </div>
</template>

<script>
    import comeback from '../../components/comeback.vue'
    import { userInfoGet, WXShare } from '../../libs/publicMethod.js'
    import { getClassifyInfos, joinCart, joinMyFindOrFoot } from '../../libs/interface'
    export default {
        components: { comeback, getClassifyInfos, joinCart, userInfoGet, joinMyFindOrFoot, WXShare },
        data() {
            return {
                goType: '',
                showShadow: false,  //点击购物车显示阴影
                mark: 0,            //缩略图下标
                imgSrc: '',         //商品图片
                collectSrc: '',     //收藏图标
                isShow: false,      //是否显示购物车
                cartOrBuy: '',      //加入购物车or立即购买
                collectedAnimation: false, //收藏动画
                isJoinCart: true,
                //购物车数据
                cart: {
                    count: 1,
                    option: '',
                    totalPrice: 0
                },
                //商品数据
                goods: {
                    isCollected: '', //是否收藏
                    id: '',
                    title: '',
                    price: 0,
                    stock: '',
                    name: '',
                    count: '',
                    specifications: '',
                    specificationsCart: [],
                    skuid: [],
                    information: '',
                    imgs: [],
                    bigimg:[]
                }
            }
        },
        methods: {
            change (i, m) {
                this.imgSrc = i
                this.mark = m
            },
            showCart () {
                this.isShow = true
                this.cartOrBuy = '加入购物车'
                this.showShadow = true
            },
            showBuy () {
                this.isShow = true
                this.cartOrBuy = '立即购买'
                this.showShadow = true
            },
            hiddenCart () {
                this.isShow = false
                this.showShadow = false
            },
            plus () {
                this.cart.count++
            },
            minus () {
                this.cart.count--
            },
            select (s) {
                this.cart.option = s
            },
            collect () {
                let vm = this
                let params = {
                    memberId: userInfoGet().id,
                    skuId: vm.goods.id,
                }
                joinMyFindOrFoot(params, (data) => {
                    if (data.code === 1) {
                        if (!vm.goods.isCollected) {
                            vm.collectedAnimation = true
                            setTimeout(() => {
                                vm.collectedAnimation = false
                            }, 400);
                        }
                        vm.goods.isCollected = !vm.goods.isCollected
                        // console.log('收藏成功')
                        vm.collectSrc = vm.goods.isCollected ? require('../../assets/other/sc2.png') : require('../../assets/other/sc.png')
                    } else {
                        // console.log('收藏失败')
                    }
                })
            },
            addToCart () {
                let vm = this
                if (vm.goods.stock < 1) {
                    vm.$toast('库存不足')
                    return
                }
                if (vm.goods.specificationsCart.length && vm.cart.option.length === 0) {
                    vm.$toast('请选择规格')
                    return
                }
                if (vm.cartOrBuy.indexOf('加入购物车') != -1) {
                    let params = {
                        memberId: userInfoGet().id,
                        skuId: vm.goods.skuid[vm.cart.option],
                        num: vm.cart.count,
                    }
                    if (!vm.goods.specificationsCart.length) {
                        params.skuId = vm.goods.id
                    }
                    joinCart(params, (data) => {
                        if (data.code === 1) {
                            vm.cart.option = ''
                            vm.$toast('添加成功')
                        } else {
                            vm.$toast(data.msg)
                        }
                    })
                }
                if (vm.cartOrBuy.indexOf('立即购买') != -1) {
                    sessionStorage.setItem('points', JSON.stringify({ points: true }))
                    // vm.$router.push('/purchase/' + vm.goods.skuid[vm.cart.option] + '/' + vm.cart.count)
                    location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx163c4c3d188f8ee5&redirect_uri=http%3A%2F%2Fwww.xiewan8.com%2FlyApp%2F%23%2Fpurchase%2F'+ vm.goods.id + '%2F' + vm.cart.count+'&response_type=code&scope=snsapi_userinfo&state=0#wechat_redirect'
                }
            }
        },
        mounted () {
            let vm = this
            document.body.style.paddingBottom = '47px'
            // if (JSON.parse(sessionStorage.getItem('coupon')).coupon) {
            if (sessionStorage.getItem('coupon')) {
                vm.isJoinCart = false
            }
            let params = {
                productId: this.$route.params.id,
                memberId: userInfoGet().id
            }
            getClassifyInfos(params, (data) => {
                if (data.code === 1) {
                    vm.goods.isCollected = data.payload.isFind
                    vm.goods.id = data.payload.sku.id
                    vm.goods.title = data.payload.product.name
                    vm.goods.price = data.payload.product.defaultPrice / 100
                    vm.goods.stock = data.payload.sku.stock
                    for (let obj of data.payload.product.skuInfos) {
                        vm.goods.specifications += obj.skuName + ' '
                        vm.goods.specificationsCart.push(obj.skuName)
                        vm.goods.skuid.push(obj.skuId)
                    }
                    vm.goods.name = data.payload.product.name
                    vm.goods.information = data.payload.sku.skuProperties
                    vm.goods.sell = data.payload.product.sell
                    vm.goods.bigimg = data.payload.sku.bigimg.split(',')
                    vm.goods.imgs = data.payload.product.banner.split(',')
                    vm.imgSrc = vm.goods.imgs[0]
                    vm.cart.totalPrice = vm.cart.count * vm.goods.price
                    vm.collectSrc = vm.goods.isCollected ? require('../../assets/other/sc2.png') : require('../../assets/other/sc.png')
                    if (data.payload.product.goods === 2) {
                        vm.isJoinCart = false
                    }
                    if (data.payload.product.goods === 1 && data.payload.product.runType === 3) {
                        vm.isJoinCart = false
                    }
                    if (sessionStorage.getItem('inviterID') !== -1) {
                        vm.goType = '/mall'
                    }
                    // 分享
                    var Sdata = {                                                          //分享请求配置
                        gotoPage: vm.$route.path,
                        //邀请者链接         
                        inviterID: userInfoGet().id,                                              //链接带的参数
                        type: 1,
                        //链接图片（小图）     
                        imgUrl: vm.goods.imgs[0],
                        //邀请话
                        shareTitle: '这是我在乐销商城淘到的好宝贝！',                        
                    }
                    WXShare.topShare(Sdata)
                }
            })
            
        },
        computed: {
            totalPrice: function () {
                return this.cart.count * this.goods.price
            },
            showPrice: function () {
                return this.goods.price.toFixed(2)
            }
        },
        watch: {
            'cart.count': function () {
                if (this.cart.count < 1) {
                    this.cart.count = 1
                }
            },
            totalPrice: function () {
                this.cart.totalPrice = this.totalPrice
            },
            'goods.specifications': function () {
                if (this.goods.specifications == 'null ') {
                    this.goods.specifications = '暂无'
                }
            },
            'goods.specificationsCart': function () {
                if (this.goods.specificationsCart[0] == null) {
                    this.goods.specificationsCart.length = 0
                }
            },
        }
    }
</script>
<style scoped>
.shadow{
    width: 100%;
    height: 100%;
    z-index: 7;
    position: fixed;
    background-color: rgba(0,0,0,.5)
}
.image{
    background-color: #fff;
}
.good-img{
    width: 100%;
    /* height: 21rem; */
    height: 18rem;
    position: relative;
}
.good-img img{
    width: 100%;
    height: 100%;
}
/* .image dd{
    position: relative;
} */
.title{
    background: rgba(45, 45, 45, .7);
    color: #fff;
    height: 2.5rem;
    line-height: 2.5rem;
    position: absolute;
    width: 96%;
    bottom: 0;
    font-size: .8rem;
    text-align: left;
    padding-left: 4%;
}
.img_small{
    display: flex;
    justify-content: center;
    white-space: nowrap;
    overflow: auto;
}
.img_small span{
    display: inline-block;
    width: 4rem;
    height: 3rem;
    margin: .2rem;
    border: 1px solid #fff;
    box-sizing: border-box;
}
.img_small img{
    width: 100%;
    height: 100%;
}
.img_small .active{
    border: 1px solid #C91F16;
}
.goods-info{
    display: flex;
    justify-content: space-around;
}
.goods-info span{
    display: inline-block;
    color: #333;
    font-size: .8rem;
}
.goods-info .red{
    color: #C91F16;
}
.goods-info>span{
    margin: .3rem 0;
}
.content{
    margin-top: .6rem;
    background-color: #fff;
    text-align: left;
}
.commodity-info{
    height: 2.2rem;
    line-height: 2.5rem;
    margin: 0 1rem;
    font-size: .8rem;
    border-bottom: 1px solid #DEDEDE;
}
.detail-info{
    padding-top: .7rem;
    font-size: .7rem;
    line-height: 1.4rem;
    color: #333;
    margin: 0 .8rem;
}
.detail-title{
    font-weight: bold;
}
.detail-img{
    width: 100%;
    display: inline-block;
    margin-top: .8rem;
    text-align: center;
}
.detail-img img{
    width: 94%;
    height: 100%;
    border-radius: 1rem;
    -moz-box-shadow: 0rem 1rem 2.5rem 0rem rgba(185,182,168,0.5); 
    -webkit-box-shadow: 0rem 1rem 2.5rem 0rem rgba(185,182,168,0.5); 
    box-shadow: 0rem 1rem 2.5rem 0rem rgba(185,182,168,0.5); 
}
.buy{
    position: fixed;
    bottom: 0;
    display: flex;
    width: 100%;
}
.collection{
    width: 26%;
    height: 3rem;
    background: linear-gradient(90deg,rgba(245,245,245,1),rgba(255,255,255,1));
    -moz-box-shadow: 0rem -2rem 5rem 0rem rgba(179,177,171,0.2);
    -webkit-box-shadow: 0rem -2rem 5rem 0rem rgba(179,177,171,0.2);
    box-shadow: 0rem -2rem 5rem 0rem rgba(179,177,171,0.2);
    color: rgb(90,90,90);
    display: flex;
    align-items: center;
    justify-content: center;
}
.shoppingcart{
    width: 31%;
    height: 3rem;
    line-height: 3rem;
    background: rgba(247,247,247,1);
    -moz-box-shadow: -0.2rem -.2rem .8rem 0rem rgb(209, 209, 209) inset; 
    -webkit-box-shadow: -0.2rem -.2rem .8rem 0rem rgb(209, 209, 209) inset; 
    box-shadow: -0.2rem -.2rem .8rem 0rem rgb(209, 209, 209) inset; 
    color: rgb(19, 13, 13);
    font-size: 1.05rem;
}
.buynow{
    width: 43%;
    height: 3rem;
    line-height: 3rem;
    background: linear-gradient(0deg,rgba(226,35,25,1),rgba(253,55,8,1));
    -moz-box-shadow: 0rem -2rem 5rem 0rem rgba(179,177,171,0.2);
    -webkit-box-shadow: 0rem -2rem 5rem 0rem rgba(179,177,171,0.2);
    box-shadow: 0rem -2rem 5rem 0rem rgba(179,177,171,0.2);
    color: #fff;
    font-size: 1.05rem;
}
.onlyBuy{
    width: 74%;
}
.collection img{
    width: 1rem;
    height: 0.9rem;
    margin: 0 .3rem .15rem 0;
}
@keyframes collect{
    from{-moz-transform: scale(1.4);
        -webkit-transform: scale(1.4);
        transform: scale(1.4);}
    to{-moz-transform: scale(1);
        -webkit-transform: scale(1);
        transform: scale(1);}
}
.collection .collected{
    -moz-animation: collect .4s ease-in;
    -webkit-animation: collect .4s ease-in;
    animation: collect .4s ease-in;
}
.collection span{
    display: inline-block;
    font-size: 0.9rem;
    -moz-transform: scale(1, 1.05);
    -webkit-transform: scale(1, 1.05);
    transform: scale(1, 1.05);
    margin-top: -.1rem;
}
.mousedown{
    background-color: red;
}
.cart{
    width: 100%;
    background-color: #fff;
    -moz-box-shadow: 0rem 1rem 2.5rem 0rem rgba(185,182,168,0.5); 
    -webkit-box-shadow: 0rem 1rem 2.5rem 0rem rgba(185,182,168,0.5); 
    box-shadow: 0rem 1rem 2.5rem 0rem rgba(185,182,168,0.5);
    position: fixed; 
    bottom: 0;
    z-index: 10;
}
.selectSpecifications{
    min-height: 11rem;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    margin-bottom: 3rem;
    padding-top: 7%;
    /* background-color: aquamarine; */
}
.selectSpecifications .cart-info{
    /* border: 1px solid blue; */
    height: 2.2rem;
    display: flex;
    align-items: center;
}
.cart-title{
    font-size: .95rem;
    color: #333;
    width: 10%;
    text-align: left;
    margin: 0 9% 0 8.5%;
    /* border: 1px solid red;; */
}
.specifications{
    /* border: 1px solid red; */
    text-align: left;
    /* border: 1px solid green; */
    display: flex;
    justify-content: space-between;
    margin-bottom: 4%;
    /* border: 1px solid purple; */
}
.specifications .cart-title{
    display: inline-block;
    /* background-color: antiquewhite; */
}
.specifications .selected{
    background-color: #E22319;
    color: #fff;
    border: 1px solid #E22319;
}
.all-specifications{
    width: 75%;
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    margin: -1.5% 5% 0 -5%;
    /* border: 1px solid red; */
}
.all-specifications span{
    display: inline-block;
    border-radius: .4rem;
    padding: .3rem .8rem;
    margin: 1% 0 1% 5%;
    white-space:nowrap;
    font-size: .8rem;
    border: 1px solid #000;
}
.count .cart-title{
    margin-right: 10.5%;
}
.count .cart-title~span{
    margin-right: .8rem;
    font-size: 1.2rem;
}
.cart .cart-count {
    background-color: #DEDEDE;
    padding: 0 1.1rem;
    height: 1.8rem;
    line-height: 1.8rem;
}
.price_{
    margin-bottom: 2%;
    font-size: 1rem;
}
.price_ .cart-title{
    margin-right: 7%;
}
.minus{
    opacity: .25;
}
.push-cart{
    display: block;
    width: 100%;
    height: 3rem;
    line-height: 3rem;
    background: linear-gradient(0deg,rgba(226,35,25,1),rgba(253,55,8,1));
    box-shadow: 0rem -2rem 5rem 0rem rgba(179,177,171,0.2);
    color: #fff;
    font-size: 1.05rem;
    position: absolute;
    bottom: 0;
}
.cart-enter-active{
    transform: translateY(0);
    transition: all .3s ease;
}
.cart-leave-active{
    transform: translateY(100%);
    transition: all .3s ease;
}
.cart-enter{
    transform: translateY(100%);
}
.cart-leave{
    transform: translateY(0);
}
</style>